﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Xml.Serialization;
using FlashCards.WebApp.Models;
using System.Text;
using FlashCards.WebApp.Utils;
using FlashCards.WebApp.DataAccessLayer;
using DotNet.Highcharts;
using DotNet.Highcharts.Options;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;

namespace FlashCards.WebApp.Controllers
{
    [Authorize]
    public class FlashcardsController : Controller
    {
        //
        // GET: /Flashcards/
        public ActionResult Index()
        {
            using (var context = new FlashCards.Model.FlashCardsEntities())
            {
                var user = context.Users.FirstOrDefault(x => x.Nick == User.Identity.Name);
                var sets = FlashcardUtils.GetUserFlashcardSets(User.Identity.Name, true);
                var model = new FlashcardsModel(user.UserId, sets, true, false);
                
                return View(model);
            }
        }

        public ActionResult EditFlashcardSet(int flashcardSetId, int userId)
        {
            var model = new FlashcardSetEditModel(flashcardSetId, userId)
            {
                Id = flashcardSetId,
                UserId = userId
            };

            return View(model);
        }

        public ActionResult CreateFlashcardSet(int userId, int? groupId)
        {
            var model = new FlashcardSetEditModel(null, userId, groupId);

            return View(model);
        }

        public ActionResult DeleteFlashcardSet(int flashcardSetId, int userId)
        {
            var set = FlashcardUtils.GetFlashcardSet(flashcardSetId, true, true);
            var model = new FlashcardSetModel(set, true, true);

            return View(model);
        }

        [HttpPost]
        public ActionResult DeleteFlashcardSet(FlashcardSetModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = UserUtils.GetUserId(User.Identity.Name);

                FlashcardUtils.DeleteFlashcardSet(model.Id, userId.Value);

                return RedirectToAction("Index");
            }
            return View("Error");
        }

        public ActionResult RepeatFlashcardSet(int flashcardSetId, int userId)
        {
            var model = new FlashcardSetEditModel(flashcardSetId, userId);

            return View(model);
        }

        public ActionResult AddExistingFlashcardToGroup(int? groupId)
        {
            if (groupId.HasValue)
            {

            }

            return View("Error");
        }

        public ActionResult ViewFlashcardProgress(int flashcardSetId, int userId)
        {
            var model = new List<Highcharts>();

            var results = FlashcardUtils.GetNetworkResult(flashcardSetId, userId);
            var flashcardsTerms = FlashcardUtils.GetFlashcardsTermsAsStrings(flashcardSetId);

            var dictionary = new Dictionary<int, List<Model.NeuralNetworkResults>>();

            foreach (var resultSet in results)
            {
                foreach (var result in resultSet.ai_NeuralNetworkResults)
                {
                    if (dictionary.ContainsKey(result.FlashcardID))
                        dictionary[result.FlashcardID].Add(result);
                    else
                        dictionary.Add(result.FlashcardID, new List<Model.NeuralNetworkResults> { result });
                }
            }

            foreach (var item in dictionary)
            {

                var chart = new DotNet.Highcharts.Highcharts("Graph" + item.Key.ToString());
                chart.SetTitle(new Title { Text = flashcardsTerms[item.Key] });
                chart.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });
                chart.SetXAxis(new XAxis { Categories = new[] { "Unknown", "Poor", "Average", "Good", "UserMark", "TotalMark" } });
                chart.SetYAxis(new YAxis { Min = 0.0, Max = 1.0 });
                var series = new List<Series>();
                foreach (var result in item.Value)
                {
                    series.Add(new Series
                    {
                        Data = new Data(new object[] { result.Unknown, result.Poor, result.Average, result.Good, result.UserMark / 3.0, NeuralNetworkUtils.CalculateFlashcardsMark(result) }),
                        Name = result.ai_NeuralNetworkResultSets.Date.ToString()
                    });
                }

                chart.SetSeries(series.ToArray());
                model.Add(chart);
            }

            return View(model);
        }
    }
}
